var GameScene = (function(){

    function GameScene(){
        Scene2D.call(this);
        this.playBall = null;
        this.playBallInvincible = null;
        this.sphere = null;
        this.collisionEff = null;
        this.ballScript = null;
        this.rotateNum = 0;
        this.bgImg = null;
        this.bgEff = null;
        // this.column = null;
        this._skinType = "black";
        this._skinTypeArr = [
            "black",
            "green",
            "yellow",
        ];
        this.isPause = false;
        this.reset();
    }
    GameScene.prototype = new Scene2D();
    
    //
    var __proto = GameScene.prototype;
    
    __proto.reset = function(){
        this.adTime = 0;
        this.score = 0;
        this.floorScore = 0;                    //当前消除每一层所加分数
        this.destroyFloorNum = 0;               //当前连续销毁的层数
        this.isSuccessive = false;              //连续消除三层后，球可以无敌一次
        this.invincibleTime = 0;                //无敌时间。捡到无敌的道具后可以无敌3秒
        this.timeInterval = 0.016;              //每一帧的时间间隔
        this.DownSpeed = Config.DownSpeed;      //下落速度
        this.floorLength = 0;                   //层高度
        this.floorHeight = 0;                   //当前高度
        this.nowFloorIndex = 0;                 //当前层数
        this.nowFloorIndexYY = 0;               //当前层数高度
        this.floorArr = [];                     //存储当前所有的层 对象
        this.fogArr = [];  
        this.floorId = 0;
        this.stageX = 0;
        this.stageY = 0;
        this.lastMovex = 0;
        
        this.nowFloorIndexYY = -1*this.nowFloorIndex*Config.FloorHeight;
        
        this.randomLevelIndex = 0;

        this.scoreAniArr = {};
        this.loopFlag = 0;
    };

    __proto.initScene = function(node){        
        if(Config.channelID == 102){
            this.scoreText.fontSize -= 60
        }
    };
    
    //更新分数
    __proto._updateScore = function(){
        var _scoreNum = this.floorScore * this.destroyFloorNum
        this.score += _scoreNum;
        this.createScoreAni(_scoreNum);
        if(Config.channelID == 102){
            this.scoreText.text = Tool.insertSpace(this.score);
        }else{
            this.scoreText.text = this.score;
        }
        
        Config.gameScore = this.score;
    };
    __proto.createScoreAni = function(_scoreNum){
        var _scoreAniText = new Laya.Text;
        _scoreAniText.x = this._scoreAniText.x;
        _scoreAniText.y = this._scoreAniText.y;
        _scoreAniText.color = this._scoreAniText.color;
        _scoreAniText.font = this._scoreAniText.font;
        _scoreAniText.fontSize = this._scoreAniText.fontSize;
        _scoreAniText.width = this._scoreAniText.width;
        _scoreAniText.height = this._scoreAniText.height;
        _scoreAniText.pivotX = this._scoreAniText.pivotX;
        _scoreAniText.pivotY = this._scoreAniText.pivotY;
        _scoreAniText.align = this._scoreAniText.align;
        _scoreAniText.text = "+"+_scoreNum+"";
        this._node.scoreNode.addChild(_scoreAniText);
        var scoreAniObj = {
            _scoreAniText : _scoreAniText,
            _life : 12,
            _fontSize : 80,
            _yy : 350,
        };
        this.scoreAniArr[(this.nowFloorIndex).toString()] = scoreAniObj;
    }
    __proto.scoreAniLoop = function(){
        for (var key in this.scoreAniArr) {
            var scoreAniObj = this.scoreAniArr[key];
            scoreAniObj._life --;
            if(scoreAniObj._life <= 0){
                scoreAniObj._scoreAniText.destroy();
                delete this.scoreAniArr[key];
                continue;
            }
            scoreAniObj._fontSize += 4;
            scoreAniObj._yy -= 20;
            scoreAniObj._scoreAniText.y = scoreAniObj._yy;
            scoreAniObj._scoreAniText.scale(scoreAniObj._fontSize/80, scoreAniObj._fontSize/80);
        }
    }
    __proto.__destroyDom = function(dom){
        if(dom && dom.destroy){
            dom.destroy();
        }
       
    }
    __proto.pauseGame = function(){
        this.isPause = !this.isPause;
        if(this.isPause){
            Audio.pauseAll();
        }else{
            Audio.resumeAll();
        }
    }
    __proto.onActive = function(from){
        //广告视频
        if(Config.isAd){
            //oppo原生广告
            if(Config.channelID == 101) {
                Laya.timer.once(1000, this, this.onOPPOAd);
            }else {
                // AdUtil.showBanner(true);
                AdUtil.loadVideo(false);
            }
        }
        if(Config.channelID == 1){
            //微信每局游戏触发垃圾回收机制
            if(wx.triggerGC){
                wx.triggerGC();
            }
        }
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
        Laya.stage.on(Laya.Event.MOUSE_UP,this,this.onMouseUp);
        //this.btnPause.on(Laya.Event.MOUSE_DOWN, this, this.pauseGame);
        this.isPause = false;
        Laya.timer.frameLoop(1, this, this.onloop);
        // this.__xxFlag = GlobalCapsule.clone();
        // GlobalFloorBox.addChild(this.__xxFlag);
        // this.__xxFlag.transform.localPosition = new Laya.Vector3(30,0,0);

        // this.__zzFlag = GlobalWood.clone();
        // GlobalFloorBox.addChild(this.__zzFlag);
        // this.__zzFlag.transform.localPosition = new Laya.Vector3(0,0,30);
        
        // Laya.timer.frameOnce(1, this, function () {
        //     this.setBallInvincible()
        // });
    };

    /**
     * 调用oppo的广告组
     */
    __proto.onOPPOAd = function() {
        if(Config.isLoadSuccess) {
            AdUtil.showBanner(true);
            AdUtil.loadVideo(false);
            return;
        }
        Laya.timer.once(1000, this, this.onOPPOAd);
    }

    //移除回掉
    __proto.onInActive = function(){
        Laya.stage.off(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
        Laya.stage.off(Laya.Event.MOUSE_UP,this,this.onMouseUp);
        Laya.timer.clearAll(this);
        for (var key in this.scoreAniArr) {
            this.scoreAniArr[key]._scoreAniText.destroy();
            delete this.scoreAniArr[key];
        }
        // for (var i = 0; i < this.fogArr.length; i++) {
        //     var _fog = this.fogArr[i]
        //     if(_fog && _fog.destroy){
        //         _fog.destroy();
        //     }
            
        // }
    };

    __proto.loadScene = function(e){
        if(this.playBall && this.playBall.destroy && !this.playBall._destroyed){
            this.playBall.destroy();
            this.playBall = null;
            this.playBallInvincible.destroy();
            this.playBallInvincible = null;
            this.collisionEff.destroy();
            this.collisionEff = null;
            this.bgImg.destroy();
            this.bgImg = null;
            this.bgEff.destroy();
            this.bgEff = null;
            this.column.destroy();
            this.column = null;
        }
        if(this.sphere){
            this.sphere.destroy();
            this.sphere = null;
        }
        GlobalFloorBox.destroyChildren();
        //添加背景
        this._skinType = this._skinTypeArr[Math.floor(Math.random()*this._skinTypeArr.length)];
        if(Config.channelID == 1){
            this._skinType = "wx"
        }
        this.changeSkin();
        this.randomLevelIndex = 0;
        this.loadGameData();
    }
    __proto.changeSkin = function(){
        
        var _bgEffIndex = 0;
        switch(this._skinType){
            case "wx":         //
                _bgEffIndex = 0;
                break;
            case "green":         //yellow
                _bgEffIndex = 0;
                break;
            case "yellow":         //black
                _bgEffIndex = 1;
                break;
            case "black":    
                _bgEffIndex = 2;
                break;    
        }
        if(this._skinType == "wx"){
            // wx 只有一套皮肤 不需要更换其他
        }else{
            // 将克隆的父体的材质更换
            GlobalColumn = GlobalScene.getChildByName("Column_"+this._skinType);
            GlobalBaffle = GlobalScene.getChildByName("Baffle_"+this._skinType);
            GlobalPlayerBall = GlobalScene.getChildByName("PlayerBall_"+this._skinType);
            GlobalSphere = GlobalScene.getChildByName("Sphere_"+this._skinType);
            GlobalCollisionEff = GlobalScene.getChildByName("Splash_"+this._skinType);
            this.sphere = GlobalSphere.clone();
            for (var i = 0; i < GlobalGroundArr.length; i++) {
                GlobalGroundArr[i].meshRender.material = GlobalScene.getChildByName("Ground_"+this._skinType).meshRender.material.clone();
            }
            for (var i = 0; i < GlobalCarpetArr.length; i++) {
                GlobalCarpetArr[i].meshRender.material = GlobalScene.getChildByName("Baffle_"+this._skinType).meshRender.material.clone();
            }
        }
        this.bgEff = GlobalBgEff[_bgEffIndex].clone();
        this.bgEff.active = true;
        this.bgEff.transform.position = new Laya.Vector3(0,-50,30);
        GlobalScene.addChild(this.bgEff);

        var bgRes = Laya.loader.getRes("game/bg_"+this._skinType +".png");
        // var bgRes = Laya.loader.getRes("game/bg_yellow.png");
        
        this.bgImg = new Laya.Sprite();
        this.bgImg.graphics.drawTexture(bgRes, 0, 0,Config.GameWidth*2,Config.GameHeight + offH);
        this.bgImg.x = -Config.GameWidth/2;
        this.bgImg.zOrder = -100;
        Laya.stage.addChild(this.bgImg);

        this.column = GlobalColumn.clone();
        this.column.transform.localScale = new Laya.Vector3(1,300,1);
        this.column.transform.position = new Laya.Vector3(0,Config.GlobalColumnPositionYY,0);
        this.column.active = true;
        GlobalScene.addChild(this.column)
        
        this.playBall = GlobalPlayerBall.clone();
        this.collisionEff = GlobalCollisionEff.clone();
        this.playBall.transform.position = new Laya.Vector3(0,Config.BallStartYY,Config.BallStartZZ);
        this.ballScript = this.playBall.addComponent(BallScript);
        this.playBall.active = true;
        GlobalScene.addChild(this.playBall);
        
        this.playBallInvincible = GlobalPlayerBallInvincible.clone();
        this.playBallInvincible.transform.position = new Laya.Vector3(0,200,Config.BallStartZZ);
        this.playBallInvincible.active = true;
        GlobalScene.addChild(this.playBallInvincible);

        
        GlobalFloorBox.active = true;
    }

    __proto.onClick = function(e){
      
    }
    __proto.onMouseDown = function(e){
        //Tool.log('-----鼠标按下-----curTouchId ： ' + e.touchId+"------stageX： " + e.stageX+"------stageY： " + e.stageY);
        //记录当前按下id
         this.curTouchId = e.touchId;
         this.stageX = e.stageX; 
         this.stageY = e.stageY;
         this.lastMovex = e.stageX;
         Laya.stage.on(Laya.Event.MOUSE_MOVE,this,this.onMouseMove);
    }
    /*鼠标抬起事件回调*/
    __proto.onMouseUp = function(e){
        //Tool.log('-----鼠标松开-----curTouchId ： ' + e.touchId+"------stageX： " + e.stageX+"------stageY： " + e.stageY);
        //如果不是上次的点击id，返回（避免多点抬起，以第一次按下id为准）
        if(this.isPause || e.touchId != this.curTouchId) return;
        var moveX = e.stageX - this.stageX;
        Laya.stage.off(Laya.Event.MOUSE_MOVE,this,this.onMouseMove);
    }
    __proto.onMouseMove = function(e){
        if(this.isPause || e.touchId != this.curTouchId) return;
        var moveX = e.stageX - this.lastMovex;
        var _moveX = moveX*Config.RotateSpeed;
        //Tool.log('-----鼠标移动-----curTouchId ： ' + e.touchId+"------stageX： " + e.stageX+"------stageY： " + e.stageY+"------moveX:  " + moveX+"------_moveX:  " + _moveX);
        GlobalFloorBox.transform.rotate(new Laya.Vector3(0,_moveX,0),true,false);
        this.rotateNum += _moveX;
        this.rotateNum = this.rotateNum%360;
        this.lastMovex = e.stageX;
	}
    __proto.loadGameData = function(){
        this.randomLevelIndex += (Math.floor(Math.random()*3)+1);
        Tool.log("--------------生成层数---------------lever : "+this.randomLevelIndex);
        if(this.randomLevelIndex >= GameLevelArr.length){
            this.randomLevelIndex = Math.floor(Math.random()*5);
        }
        var randomLevel = GameLevelArr[this.randomLevelIndex];
        var GroundType = randomLevel.GroundType.split('|');
        var GroundRotation = randomLevel.GroundRotation.split('|');
        var PropPlacement = randomLevel.PropPlacement.split('|');
        var ObstaclePlacement = randomLevel.ObstaclePlacement.split('|');
        var FogPlacement = randomLevel.FogPlacement.split('|');
        var randomStart = GroundType.length - Config.ModuleFloorNum - 1;
        randomStart = Math.floor(Math.random()*randomStart);
        if(this.randomLevelIndex <= 5){
            randomStart = 0;
        }
        var randomEnd = randomStart+Config.ModuleFloorNum;
        for (var i = randomStart; i < randomEnd; i++) {
            var floorArrItem = [];
            var GroundTypeItem = GroundType[i].split(";");
            var GroundRotationItem = GroundRotation[i].split(";");
            var PropPlacementItem = PropPlacement[i].split(";");
            var ObstaclePlacementItem = ObstaclePlacement[i].split(";");
            for (var j = 0; j < GroundTypeItem.length; j++) {
                var __groundObj = this.addOneFloor(Number(GroundTypeItem[j]),Number(GroundRotationItem[j]),PropPlacementItem[j],ObstaclePlacementItem[j],j);
                floorArrItem.push(__groundObj);
            }
            this.floorLength ++;
            this.floorHeight -= Config.FloorHeight;
            
            this.floorArr.push(floorArrItem);
        }
    }
    __proto.addOneFloor = function(GroundType,GroundRotation,PropPlacement,ObstaclePlacement,groundIndex){
        if(GroundType < 0 ) GroundType = 0;
        if(GroundType > 12 ) GroundType = 12;
        while (GroundRotation < 0) {
            GroundRotation += 360;
        }
        // layabox  网格碰撞函数有问题，得手动模拟碰撞，根据角度以及高度来进行模拟判断
        var GroundItemObj = {
            _ground_rotate:0,
            _ground_angle:0,
            _capsule_rotate:0,
            _capsule_angle:0,
            _carpet_arr :[],
            _thorn_arr :[],
            _baffle_arr :[],
            _wood_arr :[],
            __ground:null,
            __groundScript:null,
            __groundChild:[],
            __groundChildScript:[],
            __capsule:null,
        };
        // 生成普通地板
        var __GroundRotation = GroundRotation;
        var __GroundType = GroundType;
        var __localPosition = new Laya.Vector3(0,this.floorHeight,0);
        var __ground = null;

        var __rotate =  new Laya.Vector3(0,GroundRotation,0);
        var _groundChild = GlobalGroundArr[GroundType - 1].clone();
        _groundChild.active = true;
        GlobalFloorBox.addChild(_groundChild);
        _groundChild.transform.localPosition = __localPosition;
        _groundChild.transform.rotate(__rotate, true, false);
        var groundScript = _groundChild.addComponent(GroundScript);
        groundScript.setBaseData(GroundRotation,GroundType*30,this.floorHeight);
        if(__ground == null) __ground = _groundChild;

        GroundItemObj.__groundChild.push(_groundChild);
        GroundItemObj.__groundChildScript.push(groundScript);
        GroundItemObj._ground_rotate = GroundRotation;
        GroundItemObj._ground_angle = GroundType*30;
        GroundItemObj.__ground = __ground;
        GroundItemObj.__groundScript = groundScript;
        GroundItemObj.__groundChildScript.enble =  false;
        
        //生成无敌道具
        if(PropPlacement && PropPlacement != ""){
            var _groundChild = GlobalGroundArr[0].clone();
            var __offsetRotate = -30;
            _groundChild.active = true;
            __ground.addChild(_groundChild);
            _groundChild.transform.localScale = Config.GroundScale;
            _groundChild.transform.localPosition = new Laya.Vector3(0,0,0);
            _groundChild.transform.rotate(new Laya.Vector3(0,__offsetRotate,0), true, false);
            var __capsule = GlobalCapsule.clone();
            __capsule.active = true;
            _groundChild.addChild(__capsule);
            __capsule.transform.localPosition = new Laya.Vector3(15,10,4);
            __capsule.transform.localScale = Config.CapsuleScale;
            GroundItemObj._capsule_rotate = GroundRotation + __offsetRotate - (30 - Config.CapsuleAngle)/2;
            GroundItemObj._capsule_angle = Config.CapsuleAngle;
            var capsuleScript = __capsule.addComponent(CapsuleScript);
            GroundItemObj.__capsule = __capsule;
        }
        if(ObstaclePlacement && ObstaclePlacement != ""){
            var __groundCanSetPos = [];
            for (var i = 0; i < GroundType; i++) {
                __groundCanSetPos[i] = i;
            }
            var ObstaclePlacementItem = ObstaclePlacement.split("&");
            for (var i = 0; i < ObstaclePlacementItem.length; i++) {
                var ObstacleType = Number(ObstaclePlacementItem[i]);
                if(ObstacleType < 0) ObstacleType=ObstacleType*-1;
                //ground上每30度为一个位置,随机一个位置，然后在数组中删除，防止下次还随机到
                var randomPos = __groundCanSetPos[Math.floor(Math.random()*__groundCanSetPos.length)];
                __groundCanSetPos.splice(randomPos,1);
                var __offsetRotate = randomPos*-30;
                //设置一个30度的地板，用来作为载体，根据随机的位置，旋转到相应角度
                var _groundChild2 = GlobalGroundArr[0].clone();
                var __offsetRotate2 = randomPos*-30;
                // if(randomPos > 3){
                //     var __childIndex = Math.floor(randomPos/3)
                //     __ground = GroundItemObj.__groundChild[__childIndex];
                //     __offsetRotate2 += __childIndex*90;
                // }
                _groundChild2.active = true;
                __ground.addChild(_groundChild2);
                _groundChild2.transform.localPosition = new Laya.Vector3(0,0,0);
                _groundChild2.transform.rotate(new Laya.Vector3(0,__offsetRotate2,0), true, false);
                _groundChild2.transform.localScale = Config.GroundScale;
                switch (ObstacleType) {
                    case 1: //地刺
                        var __thorn = GlobalThorn.clone();
                        __thorn.active = true;
                        _groundChild2.addChild(__thorn);
                        __thorn.transform.localPosition = new Laya.Vector3(15,4.4,4);
                        __thorn.transform.rotate(new Laya.Vector3(0,-15,0), true, false);
                        __thorn.transform.localScale = Config.ThornScale;
                        var obj = {};
                        obj._rotate = GroundRotation + __offsetRotate - (30-Config.ThornAngle)/2;
                        obj._angle = Config.ThornAngle;
                        // 在底部板子上添加移动脚本，同时在脚本移动的时候更新数据
                        var thornScript = _groundChild2.addComponent(ThornScript);
                        //thornScript.setBaseData(GroundRotation+__offsetRotate,GroundRotation+GroundType*30,GroundRotation,this.floorArr.length,groundIndex,i);
                        thornScript.setBaseData(__offsetRotate,0,-GroundType*30,this.floorArr.length,groundIndex,i);
                        GroundItemObj._thorn_arr.push(obj);
                        break;
                    case 2: //红砖
                        var __carpet = GlobalCarpetArr[0].clone();
                        __carpet.active = true;
                        _groundChild2.addChild(__carpet);
                        
                        if(this._skinType == "wx"){
                            __carpet.transform.localScale = Config.CarpetScale;
                        }else{
                            __carpet.transform.localScale = Config.CarpetScale;
                        }
                        __carpet.transform.localPosition = new Laya.Vector3(0,0,0);
                        __carpet.transform.rotate(new Laya.Vector3(0,0,0), true, false);
                        var obj = {};
                        obj._rotate = GroundRotation + __offsetRotate;
                        obj._angle = 30;
                        GroundItemObj._carpet_arr.push(obj);
                        break;
                    case 3: //竖立挡板
                        var __baffle = GlobalBaffle.clone();
                        __baffle.active = true;
                        _groundChild2.addChild(__baffle);
                        __baffle.transform.localPosition = new Laya.Vector3(0,0,0);
                        __baffle.transform.rotate(new Laya.Vector3(0,90,0), true, false);
                        __baffle.transform.localScale = Config.BaffleScale;
                        var obj = {};
                        obj._rotate = GroundRotation + __offsetRotate - (30 - Config.BaffleAngle);
                        obj._angle = Config.BaffleAngle;
                        GroundItemObj._baffle_arr.push(obj);
                        break;
                    case 4: //火堆
                        var __wood = GlobalWood.clone();
                        __wood.active = true;
                        _groundChild2.addChild(__wood);
                        __wood.transform.localPosition = new Laya.Vector3(15,4.4,4);
                        //__wood.transform.rotate(new Laya.Vector3(0,-90,0), true, false);
                        __wood.transform.localScale = Config.WoodScale;
                        var obj = {};
                        obj._rotate = GroundRotation + __offsetRotate - (30 - Config.WoodAngle)/2;
                        obj._angle = Config.WoodAngle;
                        GroundItemObj._wood_arr.push(obj);
                        break;
                    default:
                        break;
                }
                
            }
            
        }

        this.floorId ++;
        return GroundItemObj;
    }
    __proto.onloop = function(){
        this.timeInterval = Laya.timer.delta/1000;
        if(this.timeInterval >= 0.032){
            this.timeInterval = 0.02
        }
        if((this.loopFlag ++)%3 == 1) this.scoreAniLoop();
        if(this.invincibleTime > 0){
            this.invincibleTime -= this.timeInterval;
            if(this.invincibleTime < 0){
                this.invincibleTime = 0;
                this.setBallStatus(false);
                this.destroyNowFloor();
            }
        }
        this.timeInterval = 0.016
        if(this.isPause) return; 
        var floorItem =  this.floorArr[this.nowFloorIndex];
        var playBallYY = this.ballScript.yy-8;
        if( playBallYY > this.nowFloorIndexYY){
            var ballHeight = playBallYY - this.nowFloorIndexYY;
            for (var i = 0; i < floorItem.length; i++) {
                if(ballHeight < Config.CapsuleHeight && this.checkCapsuleCollision(floorItem[i])){
                    this.setBallInvincible();
                    floorItem[i].__capsule.destroy();
                    floorItem[i]._capsule_rotate = 0;
                    floorItem[i]._capsule_angle = 0;
                }
                if(ballHeight < Config.BaffleHeight && this.checkBaffleCollision(floorItem[i])){
                    this.showDeadDlg();
                    return;
                }
                if(ballHeight < Config.WoodHeight && this.checkWoodCollision(floorItem[i])){
                    this.showDeadDlg();
                    return;
                }
                if(ballHeight < Config.ThornHeight && this.checkThornCollision(floorItem[i])){
                    this.showDeadDlg();
                    return;
                }
                
            }
        }
        if( playBallYY <= this.nowFloorIndexYY && !this.ballScript.inPause){
            Tool.log("------地板上的检测碰撞-----playBallYY : " + playBallYY +"--------nowFloorIndexYY : "+this.nowFloorIndexYY+"--------this.rotateNum : "+this.rotateNum);
            
            if(this.checkFloorCollision(floorItem)){
                this.ballScript.bounceBall();
                if(this.isSuccessive) {
                    this.setBallStatus(false);
                    this.destroyNowFloor();
                    this.destroyFloorNum = 0;
                    this.isSuccessive = false;
                    return false;
                }
                this.destroyFloorNum = 0;
            }else{
                if(this.isPause) return; 
                this.destroyNowFloor();
                if(this.ballScript.DownSpeed > Config.FastestSpeed * 1.5){
                    this.ballScript.DownSpeed += Config.DestroyFloorSpeed;
                }
            }
        }
    }

    __proto.checkFloorCollision = function(floorItem){
        if(this.invincibleTime > 0){
            return false;
        }
        for (var i = 0; i < floorItem.length; i++) {
            Tool.log(floorItem[i]);
            if(this.checkCarpetCollision(floorItem[i])){
                if(this.ballScript.yy < this.nowFloorIndexYY + Config.BallRadius*1.7){
                    this.ballScript.yy = this.nowFloorIndexYY + Config.BallRadius*1.7;
                    this.ballScript.ball.transform.localPosition =  new Laya.Vector3(0, this.ballScript.yy, Config.BallStartZZ);
                }
                this.showDeadDlg();
                return false;
            }
            if(this.checkGroundCollision(floorItem[i])){
                return true;
            }
        }
        return false;
    }

    __proto.checkGroundCollision = function(groundObj){
        //全部使用正坐标
        if(this.checkPlayerBallOnInterval(groundObj._ground_rotate,groundObj._ground_angle)){
            Tool.log("------碰到了普通地板-------");
            return true;
        }
        return false;
    }
    __proto.checkCarpetCollision = function(groundObj){
        if(this.isSuccessive || this.invincibleTime > 0) return false;
        //全部使用正坐标
        for (var i = 0; i < groundObj._carpet_arr.length; i++) {
            var carpetObj = groundObj._carpet_arr[i];
            
             if(this.checkPlayerBallOnInterval(carpetObj._rotate,carpetObj._angle)){
                Tool.log("------碰到了红色地板-------");
                return true;
            }
        }
        return false;
    }

    __proto.checkBaffleCollision = function(groundObj){
        if(this.isSuccessive || this.invincibleTime > 0) return false;
        //全部使用正坐标
        for (var i = 0; i < groundObj._baffle_arr.length; i++) {
            var baffleObj = groundObj._baffle_arr[i];
             if(this.checkPlayerBallOnInterval(baffleObj._rotate,baffleObj._angle)){
                Tool.log("------碰到了红色竖墙-------");
                return true;
            }
        }
        return false;
    }
    __proto.checkWoodCollision = function(groundObj){
        if(this.isSuccessive || this.invincibleTime > 0) return false;
        //全部使用正坐标
        for (var i = 0; i < groundObj._wood_arr.length; i++) {
            var woodObj = groundObj._wood_arr[i];
             if(this.checkPlayerBallOnInterval(woodObj._rotate,woodObj._angle)){
                Tool.log("------碰到了火堆-------");
                return true;
            }
        }
        return false;
    }
    __proto.checkThornCollision = function(groundObj){
        if(this.isSuccessive || this.invincibleTime > 0) return false;
        //全部使用正坐标
        for (var i = 0; i < groundObj._thorn_arr.length; i++) {
            var thornObj = groundObj._thorn_arr[i];
             if(this.checkPlayerBallOnInterval(thornObj._rotate,thornObj._angle)){
                Tool.log("------碰到了地刺-------");
                return true;
            }
        }
        return false;
    }
    __proto.checkCapsuleCollision = function(groundObj){
        //全部使用正坐标
        if(groundObj._capsule_angle == 0) return false;
        if(this.checkPlayerBallOnInterval(groundObj._capsule_rotate,groundObj._capsule_angle)){
            Tool.log("------碰到了无敌道具-------");
            return true;
        }
        return false;
    }
    __proto.checkPlayerBallOnInterval = function(rotate,angle){
        Tool.log("----------rotate : "+rotate+"---------------angle : "+angle)
        var startRotate = rotate + this.rotateNum;
        while (startRotate > 360) {
            startRotate = startRotate%360;
        }
        while (startRotate < 0) {
            startRotate += 360;
        }
        var endRotate = startRotate - angle;
        if(     
                //(endRotate >= 360 -Config.BallRadius)                                         //终点超过了球的位置
                (startRotate*endRotate < 0)                                                     //一正一负，经过了此区间
                ||(-Config.BallRadius <= startRotate && startRotate <= Config.BallRadius)       //起点在此区间
                ||(-Config.BallRadius <= endRotate && endRotate <= Config.BallRadius)           //终点在此区间
        ){
            return true;
        }
        return false;
    }
    __proto.setBallSuccessive = function () {
        this.isSuccessive = true;
        this.setBallStatus(true);
    }
    __proto.setBallInvincible = function () {
        this.invincibleTime = Config.InvincibleTime;
        this.setBallStatus(true);
        this.ballScript.showEatCapsuleEff();
    }
    __proto.setBallStatus= function (isInvincible) {
        this.isInvincible =  isInvincible;
        if(isInvincible){
            this.ballScript.ball = this.playBallInvincible;
            this.playBall.transform.position = new Laya.Vector3(0,200,Config.BallStartZZ);
        }else{
            Laya.timer.frameOnce(30,this,function(){
                this.ballScript.ball = this.playBall;
                this.playBallInvincible.transform.position = new Laya.Vector3(0,200,Config.BallStartZZ);
            })
            
        }
    }
    __proto.destroyNowFloor = function () {
        var floorItem =  this.floorArr[this.nowFloorIndex];
        // var _fog = this.fogArr[this.nowFloorIndex];
        // if(_fog && _fog.destroy){
        //    _fog.destroy();
        // }
        for (var i = 0; i < floorItem.length; i++) {
            var groundObj = floorItem[i];
            //groundObj.__groundScript.destroySelf();
            for (var j = 0; j < groundObj.__groundChildScript.length; j++) {
                groundObj.__groundChildScript[j].enble =  true;
                groundObj.__groundChildScript[j].destroySelf();
            }
        }   
        //Audio.playVoice("Audios/smallCrush.wav",1);
        if(this.nowFloorIndex%15 == 0){
            this.floorScore ++;
        }
        this.nowFloorIndex ++;
        this.nowFloorIndexYY = -1*this.nowFloorIndex*Config.FloorHeight;
        this.destroyFloorNum ++;
        if(this.destroyFloorNum >= 3 && !this.isSuccessive && this.invincibleTime <= 0){
            this.setBallSuccessive();
        }
        this._updateScore();

        if(this.floorLength - this.nowFloorIndex <= 5){
            this.loadGameData();
        }
    }
    __proto.showDeadDlg = function(){
        this.isPause = true;
        Tool.log("---------游戏结束，显示死亡弹窗------------");
        Laya.stage.off(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
        Laya.stage.off(Laya.Event.MOUSE_UP,this,this.onMouseUp);

        /**
         * vivo没有视频广告所以直接结束游戏
         */
        if(Config.channelID == 102) {
            this.gameEnd();
            return;
        }
        if(!Config.isAd){
            this.gameEnd();
        }else{
            if(Config.channelID == 100){
                this.gameEnd();
                AdUtil.showInterstitialAd();
            }else{
                if(Config.isLoadSuccess && this.adTime < 3){
                    this.deadDlg = new ReviveDlg(this.gameRevive.bind(this),this.gameEnd.bind(this));
                    if(Config.channelID == 101) {
                        Config._playRelive = this.deadDlg;
                    }
                    this.deadDlg.show();
                }else{
                    this.gameEnd();
                }
            }
        }
        
    }
    __proto.gameRevive = function(){
        this.adTime ++;
        if(this.deadDlg && this.deadDlg.close){
           this.deadDlg.close();
        }
        this.ballScript.yy += 2;
        this.isPause = false;
        this.setBallInvincible();
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
        Laya.stage.on(Laya.Event.MOUSE_UP,this,this.onMouseUp);
        
    }
    __proto.gameEnd = function(){
        var lizi_1 = this.playBall.getChildByName("lizi_1");
        var invincible_lizi_1 = this.playBallInvincible.getChildByName("lizi_1");
        if(lizi_1 && lizi_1.destroy && !lizi_1._destroyed){
            lizi_1.destroy();
            invincible_lizi_1.destroy();
        }
        SceneManager.toggleAction("game_end");
    }
    
    __proto.resetGame = function(){
        Tool.log("---------重新开始游戏------------");
        this._node.scoreNode.destroyChildren();
        GlobalFloorBox.transform.rotate(new Laya.Vector3(0,-this.rotateNum,0),true,false);
        this.rotateNum = 0;
        this.scoreText.text = "0";
        GlobalCamera.transform.position = new Laya.Vector3(0,Config.GlobalCameraPositionYY,Config.GlobalCameraPositionZZ);
        if(this.playBall){
            this.setBallStatus(false);
            this.ballScript.resetBall();
        }
        this.reset();
        this.loadScene();
        this.isPause = true;
        Audio.resumeAll();
    }
     
    return GameScene;
})();